package stream

import org.apache.spark.sql.catalyst.plans.logical.Sample
import org.apache.spark.storage.StorageLevel
import org.apache.spark.streaming.api.java.JavaDStream.fromDStream
import org.apache.spark.streaming.{Duration, Durations, StreamingContext}
import org.apache.spark.{SparkConf, SparkContext}

object UpdateState {
  def main(args: Array[String]): Unit = {
    val context = new SparkContext(new SparkConf().setMaster("local[*]").setAppName("stream"))
    val ssc = new StreamingContext(context, Durations.seconds(5))


    val sts = ssc.socketTextStream("192.168.205.102", 8889)
//    sts.flatMap(line=>{
//      val sp = line.split(",")
//      sp
//    }).map(word=>{
//    }).reduceByKey((x,y)=>{x+y})
//      (word,1)
//      .print()
    val res = sts.flatMap(_.split(","))
      .map(word => (word, 1))
      .reduceByKey((x, y) => x + y)
      .print()

    ssc.start()
    ssc.awaitTermination()
    ssc.stop()

  }

}
